package com.wetao.module_homework.adapter;


import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wetao.lib_common.bean.response.StudentExamQuestion;
import com.wetao.lib_common.bean.vo.QuestionNumShow;
import com.wetao.lib_common.finalParam.QuestionIsRight;
import com.wetao.lib_common.greendao.model.AssistantQuestion;
import com.wetao.module_homework.R;
import com.wetao.module_homework.R2;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class HomeworkDetailNumAdapter extends RecyclerView.Adapter{
    private List<StudentExamQuestion> mData= new ArrayList<>();
    private int mCurPosition = -1;
    public HomeworkDetailNumAdapter() {
    }

    public void updateData(List<StudentExamQuestion> data) {
        mCurPosition = 0; //重置课时时，需要重置
        mData = data;
        notifyDataSetChanged();
    }

    public void setmCurPosition(int position) {
        if(mCurPosition != position) {
            mCurPosition = position;
            notifyDataSetChanged();
            if(listener != null) {
                //listener.onPositionChange(mCurPosition);
                if(mCurPosition == mData.size() - 1) {
                    listener.onIsLastQuestion(true);
                } else {
                    listener.onIsLastQuestion(false);
                }
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == 1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.homework_detail_num_item, parent, false);
            return new DetailNumHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof DetailNumHolder) {
            ((DetailNumHolder)holder).bindData(position);
        }
    }

    @Override
    public int getItemCount() {
        if(mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }


    public class DetailNumHolder extends RecyclerView.ViewHolder{

        @BindView(R2.id.tv_num)
        TextView tv_num;

        @BindView(R2.id.v_dian)
        View v_dian;

        public DetailNumHolder( View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(int position) {
            clickListener(position);
            tv_num.setText(mData.get(position).getQuestionNo() + "");
            if(mData.get(position).getIsRight() != null
                    && mData.get(position).getIsRight().intValue() != QuestionIsRight.QUESTION_IS_RIGHT){
                v_dian.setVisibility(View.VISIBLE);
            } else {
                v_dian.setVisibility(View.INVISIBLE);
            }
            if(position == mCurPosition) {
                tv_num.setTextColor(Color.parseColor("#FFFFFF"));
                tv_num.setBackgroundResource(R.drawable.common_question_num_3);
            } else {
                if(mData.get(position).getIsRight() != null
                        && mData.get(position).getIsRight().intValue() != QuestionIsRight.QUESTION_IS_RIGHT) {
                    tv_num.setTextColor(Color.parseColor("#FF4A55"));
                    tv_num.setBackground(null);
                } else {
                    tv_num.setTextColor(Color.parseColor("#333333"));
                    tv_num.setBackground(null);
                }

            }
        }
        private void clickListener(int position) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null && mCurPosition != position) {
                        setmCurPosition(position);
                        //mCurPosition = position;
                        //notifyDataSetChanged();
                        listener.onItemClick(position);
                    }
                }
            });
        }
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onItemClick(int position);
        void onPositionChange(int position);
        void onIsLastQuestion(boolean isLastQuestion);
    }
    private OnItemClickListener listener;
    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
